﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;


namespace HAVNet.CoreTesting
{
    public class Trace
    {
        static object       _lock = new object();
        static SerialPort   _port = null;

        public static SerialPort damePort ()
        {
            if (_port == null)
            {
                _port = new SerialPort("COM4:", 115200);
                _port.Open();
            }

            return _port;
        }

        public static void enterMethod (int t, string tn, string nombre, string retval, string @params)
        {
            lock (_lock)
            {
                damePort().Write(DateTime.Now.ToLongTimeString() + ":" + Environment.TickCount + " ");
                damePort().WriteLine(string.Format("Thread:[id={0}, name={1}] Meth:[{3} {2}] Prms:[{4}]\r\n", t, tn, nombre, retval, @params));
            }
        }

        public static string paramProcessor (object o)
        {
            if (o == null)
                return "NULL";

            if ((o is System.Collections.IEnumerable) && (! (o is string))) {
                if (o is System.Array) {
                    if (((System.Array)o).Length == 0) {
                        return "<Array Vacio>";
                    } else {
                        return "<Array con " + ((System.Array)o).Length + " elems>";
                    }
                } else {
                    return "<Algo IEnumerable>";
                }
            }

            return o.ToString();
        }
    }
}
